package com.zcoj.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcoj.core.model.PageData;
import com.zcoj.core.model.PageWrap;
import com.zcoj.mapper.TrainingRegisterMapper;
import com.zcoj.pojo.Dto.TrainingRegister;
import com.zcoj.service.TrainingRegisterService;
import com.zcoj.util.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Service实现
 * @author 剩xia的盛夏
 * @date 2023/02/24 16:16
 */
@Service
public class TrainingRegisterServiceImpl implements TrainingRegisterService {

    @Autowired
    private TrainingRegisterMapper trainingRegisterMapper;

    @Override
    public Integer create(TrainingRegister trainingRegister) {
        trainingRegisterMapper.insert(trainingRegister);
        return trainingRegister.getId();
    }

    @Override
    public void deleteById(Long id) {
        trainingRegisterMapper.deleteById(id);
    }

    @Override
    public void deleteByIdInBatch(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) return;
        trainingRegisterMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateById(TrainingRegister trainingRegister) {
        trainingRegisterMapper.updateById(trainingRegister);
    }

    @Override
    public void updateByIdInBatch(List<TrainingRegister> trainingRegisters) {
        if (CollectionUtils.isEmpty(trainingRegisters)) return;
        for (TrainingRegister trainingRegister: trainingRegisters) {
            this.updateById(trainingRegister);
        }
    }

    @Override
    public TrainingRegister findById(Long id) {
        return trainingRegisterMapper.selectById(id);
    }

    @Override
    public TrainingRegister findOne(TrainingRegister trainingRegister) {
        Wrapper<TrainingRegister> wrapper = new QueryWrapper<>(trainingRegister);
        return trainingRegisterMapper.selectOne(wrapper);
    }

    @Override
    public List<TrainingRegister> findList(TrainingRegister trainingRegister) {
        Wrapper<TrainingRegister> wrapper = new QueryWrapper<>(trainingRegister);
        return trainingRegisterMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<TrainingRegister> findPage(PageWrap<TrainingRegister> pageWrap) {
        IPage<TrainingRegister> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<TrainingRegister> queryWrapper = new QueryWrapper<>(WrapperUtil.blankToNull(pageWrap.getModel()));
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase("DESC")) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(trainingRegisterMapper.selectPage(page, queryWrapper));
    }

    @Override
    public long count(TrainingRegister trainingRegister) {
        Wrapper<TrainingRegister> wrapper = new QueryWrapper<>(trainingRegister);
        return trainingRegisterMapper.selectCount(wrapper);
    }
}
